<template>
  <div id="app">
    <keep-alive>
      <router-view v-if="$route.meta.keepAlive"
                   :shop-info="shopInfo"
                   :discount="+discount"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"
                 :shop-info="shopInfo"
                 :discount="+discount"></router-view>
  </div>
</template>

<script>
import bus from './store/bus.js'
export default {
  name: 'App',
  data () {
    return {
      shopInfo: {},
      discount: '',
    }
  },
  created () {
    this.getShopInfo()
    this.getDiscount()
  },
  methods: {
    getShopInfo () {
      this.$http.get(`${this.$REQ_URL}ctrl=shop&action=jmGetShopInfo&id=${this.$SHOP_ID}`)
        .then(res => {
          this.shopInfo = res.data
          document.title = this.shopInfo.shopname;
          bus.$emit('Home_country', res.data.shoplan)
        })
        .catch(err => {
        })
    },
    getDiscount () {
      const code = window.location.href.split('=')[1] ? window.location.href.split('=')[1] : '';
      const phone = localStorage.getItem('user') ? (JSON.parse(localStorage.getItem('user'))).phone : ''
      this.$http.get(`http://order.huaqiaobang.com/index.php?ctrl=order&action=get_discount_rate&phone=${phone}&shopid=${this.$SHOP_ID}&rate=${code}`)
        .then(res => {
          if (res.data.state === 200) {
            this.discount = res.data.data
          }
        })
        .catch(err => {

        })
    },
  }
}
</script>

<style>
#app {
  font-family: PingFangSC-Regular;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
</style>
